package org.commentservice.service.impl;

import org.apache.log4j.Logger;
import org.commentservice.dao.CommentMapper;
import org.commentservice.entity.Comment;
import org.commentservice.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    private static final Logger logger = Logger.getLogger(CommentServiceImpl.class);

    @Autowired
    private CommentMapper commentMapper;

    @Override
    public int add(Comment comment){
        logger.info("Adding new comment for house ID: " + comment.getHouseId() + " by tenant ID: " + comment.getTenantId());
        int result = commentMapper.insert(comment);
        logger.info("Comment added with ID: " + comment.getCommentId() + ", result: " + result);
        return result;
    }

    @Override
    public int deleteById(long commentId){
        logger.info("Deleting comment by ID: " + commentId);
        int result = commentMapper.deleteById(commentId);
        logger.info("Comment deletion result: " + result);
        return result;
    }

    @Override
    public int update(Comment comment){
        logger.info("Updating comment with ID: " + comment.getCommentId());
        int result = commentMapper.update(comment);
        logger.info("Comment update result: " + result);
        return result;
    }

    @Override
    public List<Comment> findByHouseId(long houseId){
        logger.info("Finding comments by house ID: " + houseId);
        List<Comment> comments = commentMapper.findByHouseId(houseId);
        logger.debug("Found " + comments.size() + " comments for house ID: " + houseId);
        return comments;
    }

    @Override
    public List<Comment> findByTenantId(long tenantId){
        logger.info("Finding comments by tenant ID: " + tenantId);
        List<Comment> comments = commentMapper.findByTenantId(tenantId);
        logger.debug("Found " + comments.size() + " comments for tenant ID: " + tenantId);
        return comments;
    }

    @Override
    public Comment findByHouseIdAndTenantId(long houseId, long tenantId){
        logger.info("Finding comment by house ID: " + houseId + " and tenant ID: " + tenantId);
        Comment comment = commentMapper.findByHouseIdAndTenantId(houseId, tenantId);
        logger.debug("Found comment: " + comment);
        return comment;
    }

    @Override
    public List<Comment> findAll(){
        logger.info("Retrieving all comments");
        List<Comment> comments = commentMapper.findAll();
        logger.debug("Found " + comments.size() + " comments");
        return comments;
    }
}
